package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerInfoFeignClient customerInfoFeignClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public String login(String code) {
        //远程调用，获取用户id
        Result<Long> loginResult = customerInfoFeignClient.login(code);
        Integer codeResult = loginResult.getCode();
        log.info("状态码为:{}",codeResult);
        if (codeResult != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        Long userId = loginResult.getData();
        if (userId == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //生成token字符串
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        //将信息存入redis
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,
                userId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                TimeUnit.SECONDS
                );
        return token;
    }

    @Override
    public CustomerLoginVo getCustomerLoginInfo() {
        //根据token查询redis
//        String key = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
//        String customerId = redisTemplate.opsForValue().get(key);
//        if (!StringUtils.hasText(customerId)){
//            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
//        }
        //从ThreadLocal获取用户id
        Long customerId = AuthContextHolder.getUserId();
        //远程调用
        Result<CustomerLoginVo> customerInfo = customerInfoFeignClient.getCustomerInfo(customerId);
        Integer code = customerInfo.getCode();
        if (code != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        CustomerLoginVo customerLoginVo = customerInfo.getData();
        if (customerLoginVo == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return customerLoginVo;
    }

    @Override
    public Boolean updateWxPhone(UpdateWxPhoneForm updateWxPhoneForm) {
        Result<Boolean> booleanResult = customerInfoFeignClient.updateWxPhoneNumber(updateWxPhoneForm);
        Boolean result = booleanResult.getData();
        return result;
    }
}
